package servlet;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Employee;
import entity.Salary;
import service.Service;

/**
 * Servlet implementation class PutBonusAndPunishServlet
 */
@WebServlet("/PutBonusAndPunishServlet")
public class PutBonusAndPunishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PutBonusAndPunishServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int empId = Integer.parseInt(request.getParameter("empId"));
		int leaveDay = Integer.parseInt(request.getParameter("leaveDay"));
		int lateDay = Integer.parseInt(request.getParameter("lateDay"));
		int absentDay = Integer.parseInt(request.getParameter("absentDay"));
		double deductionSal = Double.parseDouble(request.getParameter("deduction"));
		double dayBonus = Double.parseDouble(request.getParameter("dayBonus"));
		double yearBonus = Double.parseDouble(request.getParameter("yearBonus"));
		double bonusSal = dayBonus + yearBonus;
		Employee emp = new Employee();
		Service s = new Service();
		emp = s.searchEmployeeById(empId);
		 DecimalFormat df = new DecimalFormat(".00");
		double leaveSal = leaveDay * emp.getLeaveParam();
		double lateSal = lateDay * emp.getLateParam();
		double absentSal = absentDay * emp.getAbsentParam();
		df.format(leaveSal);
		df.format(lateSal);
		df.format(absentSal);
		Service s1 = new Service();
		Salary salary = new Salary();
		salary.setEmpId(empId);
		salary.setDeductionSal(deductionSal);
		salary.setBonusSal(bonusSal);
		salary.setLeaveSal(leaveSal);
		salary.setLateSal(lateSal);
		salary.setAbsentSal(absentSal);
		if(s1.setBonusAndPunish(salary)) {
			request.getRequestDispatcher("a_other/success.jsp").forward(request, response);
		}
	}

}
